package npu.dce.andcontact1;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import android.database.Cursor;

class Group_Join_Client_Off1 implements Runnable{
	 
	private String CLIENTIP = "10.0.2.2";
	private int CLIENTPORT = 6000;
	
    private String StrVersion, StrTypes, StrOwner, StrGivenName, StrMiddleName, StrFamilyName, StrGender, StrSpinPhone, StrPhone,
    	   StrSpinEmail, StrEmail, StrSpinIM, StrIM, StrSpinPostalAddr, StrStreet, StrPOBox, StrCity, StrState,
    	   StrZipCode, StrCountry, StrSpinSNS, StrSNS, StrSpinOrg1, StrOrg1, StrSpinOrg2, StrOrg2, StrNotes,
    	   StrTime;
    
    private byte[] photo;
    
    private UpdatePacket update;
    private ContactDBAdapter mDbHelper_server;
    private InetAddress iadd;
    private DatagramSocket socket;
    private Cursor cursor;
    private DatagramPacket packet;
    
    public Group_Join_Client_Off1(ContactDBAdapter mDbHelper) {     
    	mDbHelper_server = mDbHelper;
    }

    //@Override
    public void run() {
         	
	// TODO Auto-generated method stub
	try{
		
		iadd = InetAddress.getByName(CLIENTIP);
		socket = new DatagramSocket();
		
		mDbHelper_server.open();
		cursor = mDbHelper_server.fetchAllContacts();
 	
		if(cursor != null){
			if(cursor.moveToFirst()){

				do{ 
	
					StrVersion = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_VERSION));	
					StrTypes = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_TYPES));	
					StrOwner = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_OWNER));	
					StrGivenName = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_GIVENNAME));
					StrMiddleName = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_MIDDLENAME));
					StrFamilyName = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_FAMILYNAME));
					StrGender = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_GENDER));
					StrSpinPhone = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_SPINPHONE));
					StrPhone = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_PHONE));
					StrSpinEmail = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_SPINEMAIL));
					StrEmail = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_EMAIL));
					StrSpinIM = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_SPINIM));
					StrIM = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_IM));
					StrSpinPostalAddr = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_SPINADDR));
					StrStreet = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_STREET));
					StrPOBox = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_POBOX));
					StrCity = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_CITY));
					StrState = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_STATE));
					StrZipCode = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_ZIPCODE));
					StrCountry = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_COUNTRY));
					StrSpinSNS = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_SPINSNS));
					StrSNS = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_SNS));
					StrSpinOrg1 = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_SPINORG1));
					StrOrg1 = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_ORG1));
					StrSpinOrg2 = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_SPINORG2));
					StrOrg2 = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_ORG2));            			
					StrNotes = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_NOTES));
					StrTime = cursor.getString(cursor.getColumnIndex(ContactDBAdapter.KEY_TIME));
					photo = cursor.getBlob(cursor.getColumnIndex(ContactDBAdapter.KEY_PHOTO));
						
					update = new UpdatePacket(photo.length);
					
					update.setVersion(StrVersion);					
					update.setTypes(StrTypes);		
					update.setOwner(StrOwner);
					update.setGivenName(StrGivenName);					
					update.setMiddleName(StrMiddleName);  					
					update.setFamilyName(StrFamilyName); 
					update.setGender(StrGender);
					update.setSpinPhone(StrSpinPhone);
					update.setPhone(StrPhone);					
					update.setSpinEmail(StrSpinEmail);					
					update.setEmail(StrEmail);  					
					update.setSpinIM(StrSpinIM);					
					update.setIM(StrIM);					
					update.setSpinPostalAddr(StrSpinPostalAddr);					
					update.setStreet(StrStreet);        
					update.setPOBox(StrPOBox);    
					update.setCity(StrCity);  
					update.setState(StrState);
					update.setZipCode(StrZipCode);               
					update.setCountry(StrCountry);					
					update.setSpinSNS(StrSpinSNS);					
					update.setSNS(StrSNS);     
					update.setSpinOrg1(StrSpinOrg1);					
					update.setOrg1(StrOrg1);
					update.setSpinOrg2(StrSpinOrg2);
					update.setOrg2(StrOrg2);					
					update.setNotes(StrNotes);
					update.setTime(StrTime);					
					update.setPhotoSize(Integer.toString(photo.length));												 
					update.setPhoto(photo);
					
					packet = new DatagramPacket( update.update_packet, update.update_packet.length, iadd, CLIENTPORT);
					
					socket.send(packet);  					
					              
				} while(cursor.moveToNext());                        	
     	
			}//if                 	
		}	
		
		cursor.close(); 
		socket.close();

		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}         
    } //run
} //Group_Join_Client_Off1 